<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.docs.webservices.DocServiceImpl" %>
<%@ page import="weaver.docs.webservices.DocAttachment" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.docs.webservices.DocInfo" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="weaver.general.StaticObj" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="weaver.interfaces.datasource.DataSource" %>
<%@ page import="java.sql.Clob" %>
<%@ page import="java.io.*" %>
<script>
</script>
<%
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();
    int formmodeid = 131;
    String GUID = "00b3048c-478d-4573-a0d4-e14f5cf9c066";
    Connection conn = null;
    DataSource ds = (DataSource) StaticObj.getServiceByFullname( ("datasource.xfcsfj"), DataSource.class );
    try {
        conn = ds.getConnection();
        String sql = "select AttachGuid,AttachFileName,Content,DocumentType from Frame_AttachStorage  where AttachGuid =?";
        PreparedStatement pstm = conn.prepareStatement( sql );
        pstm.setString( 1, GUID );
        ResultSet rs = pstm.executeQuery();
        out.print( ">>>>>>>>开始执行>>>>>>" + "<br/>" );
        if (rs.next()) {
            String AttachFileName = Util.null2String( rs.getString( "AttachFileName" ) );//工号
            out.print( ">>>>>>>>AttachFileName：" + AttachFileName + "<br/>" );
            byte[] content = rs.getBytes( "Content" );
            out.print( ">>>>>>>>111111<br/>" );
            out.print( ">>>>>>>>content：" + content + "<br/>" );
            int docid = createDoc( userid, content, AttachFileName );
            out.print( ">>>>>>>>docid：" + docid + "<br/>" );

        }
    } catch (Exception e) {
        e.printStackTrace();
        out.print( "执行异常，请查看日志！！！" );
    }
%>
<%!
    //小文件创建文档
    public static int createDoc(int userid, byte[] content, String rawFileName) {
        BaseBean log = new BaseBean();
        // 上传附件，创建html文档
        int docid = -1;
        DocServiceImpl docserviceimpl = new DocServiceImpl();
        DocAttachment[] docattachment = new DocAttachment[1];
        String docsubject = ""; //文档标题
        String keyword = ""; //文档内容
        String filetype = "";
        String filename = "";
        if (rawFileName.lastIndexOf( "." ) > -1) {
            filetype = rawFileName.substring( rawFileName.lastIndexOf( "." ) + 1 );
            filename = rawFileName.substring( 0, rawFileName.lastIndexOf( "." ) );
        }
        try {
            DocAttachment da = new DocAttachment();
            da.setFilename( filename + "." + filetype );//附件标题
            da.setFilecontent( Base64.encode(content) ); //附件内容
            da.setFiletype( filetype ); //imagefiletype
            da.setImagefilename( filename + "." + filetype );// 附件标题
            da.setIszip( 1 );//是否压缩
            da.setIsAesEncrype( 0 );//是否加密
            da.setAesCode( "0" );
            da.setIsextfile( "1" ); //附件是否是office类型
            if (filetype.equalsIgnoreCase( "doc" )) {
                da.setDocfiletype( "3" );
            } else if (filetype.equalsIgnoreCase( "xls" )) {
                da.setDocfiletype( "4" );
            } else if (filetype.equalsIgnoreCase( "ppt" )) {
                da.setDocfiletype( "5" );
            } else if (filetype.equalsIgnoreCase( "wps" )) {
                da.setDocfiletype( "6" );
            } else if (filetype.equalsIgnoreCase( "docx" )) {
                da.setDocfiletype( "7" );
            } else if (filetype.equalsIgnoreCase( "xlsx" )) {
                da.setDocfiletype( "8" );
            } else if (filetype.equalsIgnoreCase( "pptx" )) {
                da.setDocfiletype( "9" );
            } else {
                da.setDocfiletype( "2" );
            }
            docattachment[0] = da;
            docsubject = filename;
            keyword = filename;
            DocInfo doc = new DocInfo();//创建文档
            doc.setMaincategory( 37 );//主目录id
            doc.setSubcategory( 62 );//分目录id
            //doc.setSeccategory( 26541 );//子目录id
            doc.setDocSubject( docsubject );//标题
            doc.setDoccontent( "" );//文档内容
            doc.setKeyword( keyword );
            doc.setAttachments( docattachment );
            if (!"".equals( userid )) {
                User user = new User( userid );
                docid = docserviceimpl.createDocByUser( doc, user );
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.writeLog( "e:" + e );
        }
        return docid;
    }
    //大文件创建文档
    public static int createDoc(int userid, String filePath,String rawFileName,int count){
        BaseBean log = new BaseBean();
        byte[] content = new byte[102400];
        // 上传附件，创建html文档
        content = null;
        try {
            int byteread;
            byte data[] = new byte[1024];
            InputStream input = new FileInputStream(new File(filePath));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((byteread = input.read(data)) != -1) {
                out.write(data, 0, byteread);
                out.flush();
            }
            content = out.toByteArray();
            input.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int docid = -1;
        DocServiceImpl docserviceimpl = new DocServiceImpl();
        DocAttachment[] docattachment = new DocAttachment[1];
        String docsubject = ""; //文档标题
        String keyword = ""; //文档内容
        String filetype = "";
        String filename = "";
        if (rawFileName.lastIndexOf( "." ) > -1) {
            filetype = rawFileName.substring( rawFileName.lastIndexOf( "." ) + 1 );
            filename = rawFileName.substring( 0, rawFileName.lastIndexOf( "." ) );
        }
        try {
            DocAttachment da = new DocAttachment();
            da.setFilename( filename+"_"+count+"."+filetype);//附件标题
            da.setFilecontent( Base64.encode(content)); //附件内容
            da.setFiletype( filetype ); //imagefiletype
            da.setImagefilename( filename+"_"+count+"."+filetype );// 附件标题
            da.setIszip( 1 );//是否压缩
            da.setIsAesEncrype( 0 );//是否加密
            da.setAesCode( "0" );
            da.setIsextfile( "1" ); //附件是否是office类型
            if (filetype.equalsIgnoreCase( "doc" )) {
                da.setDocfiletype( "3" );
            } else if (filetype.equalsIgnoreCase( "xls" )) {
                da.setDocfiletype( "4" );
            } else if (filetype.equalsIgnoreCase( "ppt" )) {
                da.setDocfiletype( "5" );
            } else if (filetype.equalsIgnoreCase( "wps" )) {
                da.setDocfiletype( "6" );
            } else if (filetype.equalsIgnoreCase( "docx" )) {
                da.setDocfiletype( "7" );
            } else if (filetype.equalsIgnoreCase( "xlsx" )) {
                da.setDocfiletype( "8" );
            } else if (filetype.equalsIgnoreCase( "pptx" )) {
                da.setDocfiletype( "9" );
            } else {
                da.setDocfiletype( "2" );
            }
            docattachment[0] = da;
            docsubject = filename;
            keyword = filename;
            DocInfo doc = new DocInfo();//创建文档
            doc.setMaincategory( 37 );//主目录id
            doc.setSubcategory( 62 );//分目录id
            //doc.setSeccategory( 26541 );//子目录id
            doc.setDocSubject( docsubject );//标题
            doc.setDoccontent( "" );//文档内容
            doc.setKeyword( keyword );
            doc.setAttachments( docattachment );
            if (!"".equals( userid )) {
                User user = new User( userid );
                docid = docserviceimpl.createDocByUser( doc, user );
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.writeLog( "e:" + e );
        }
        return docid;
    }
%>